from struct import pack,unpack,calcsize
import math

def byte2bit(str):
    return bin(int(str,16))[2:].rjust(8,"0")

r_number = 0

def UnpackData(Byte):
    Byte = Byte
    location = 0
    def r_unpack(fmt):
        nonlocal location
        global r_number
        if type(fmt) == int:
            value = "0x" + Byte[location:location + fmt].hex()
            location += fmt
            r_number += fmt
            return value
        if "x" in fmt:
            unpack(fmt,Byte[location:calcsize(fmt) + location])
            location += calcsize(fmt)
            r_number += calcsize(fmt)
            return
        elif fmt == "z":
            count = Byte[location:].find(b'\x00')
            fmt = '%dsx' % (count)
        value = unpack(fmt,Byte[location:calcsize(fmt) + location])[0]
        location += calcsize(fmt)
        r_number += calcsize(fmt)
        if type(value) == bytes:
            value = value.decode()
        return value
    return r_unpack

def UnpackDoodad_doo(Byte):
    CU = UnpackData(Byte)
    obj = {
        "magic_number" : CU('4s'),
        "format_version" : CU('i'),
        "format_subversion" : CU('i'),
        "doodads_count" : CU('i'),
    }
    if obj['doodads_count'] > 0:
        obj['doodads'] = []
        for _ in range(obj['doodads_count']):
            doodad = {
                'ID' : CU('4s'),
                '样式' : CU('i'),
                '位置 X' : CU('f'),
                '位置 Y' : CU('f'),
                '位置 Z' : CU('f'),
                '面向角度' : CU('f'),
                '缩放 X' : CU('f'),
                '缩放 Y' : CU('f'),
                '缩放 Z' : CU('f'),
                # skin_id : CU('4s'), 这重制版的 这里不搞
                '可见' : CU('b'),
                # 可见说明:
                #     0 = 不可见且非实心树
                #     1 = 可见但非实心树
                #     2 = 普通树（可见且实心）
                '生命' : CU('b'),
                '掉落表类别' : CU('i'),
                '自定义掉落表数量' : CU('i'),
            }
            
            if doodad['自定义掉落表数量'] > 0:
                doodad['自定义掉落表'] = []
                for _ in range(doodad['自定义掉落表数量']):
                    ItemSet = {'物品数量' : CU('i')}
                    if ItemSet['物品数量'] > 0:
                        ItemSet['物品'] = []
                        for _ in range(ItemSet['物品数量']):
                            ItemSet['物品'].append({
                                'ID' : CU('4s'),
                                '几率' : CU('i'),
                            })
                    doodad['自定义掉落表'].append(ItemSet)

            doodad['创建编号'] = CU('i')
            obj['doodads'].append(doodad)
    obj['terrain_format_version'] = CU('i')
    obj['terrain_doodads_count'] = CU('i')# 这玩意不知道是啥 不是装饰物和可破坏物的一部分
    if obj['terrain_doodads_count'] > 0:
        obj['terrain doodads'] = []
        for _ in range(obj['terrain_doodads_count']):
            obj['terrain doodads'].append({
                'ID' : CU('4s'),
                '样式' : CU('i'),
                '位置 X' : CU('i'),
                '位置 Y' : CU('i'),
            })
    return obj

def UnpackUnit_doo(Byte):
    CU = UnpackData(Byte)
    obj = {
        'magic_number' : CU('4s'),
        'format_version' : CU('i'),
        'subversion' : CU('i'),
        'unit_count' : CU('i'),
        'units' : []
    }
    for _ in range(obj['unit_count']):
        unit = {
            'ID' : CU('4s'),
            '样式' : CU('i'),
            '位置 X' : CU('f'),
            '位置 Y' : CU('f'),
            '位置 Z' : CU('f'),
            '面向角度' : CU('f'),
            '缩放 X' : CU('f'),
            '缩放 Y' : CU('f'),
            '缩放 Z' : CU('f'),
            '可见' : CU('b'),
            '玩家' : CU('i'),
            'unknown0' : CU('b'),
            'unknown1' : CU('b'),
            '生命' : CU('i'),
            '魔法' : CU('i'),
        }
        if obj['subversion'] >= 11:
            unit['掉落列表'] = CU('i')
        unit['掉落数量'] = CU('i')
        if unit['掉落数量'] > 0:
            unit['掉落'] = []
            for _ in range(unit['掉落数量']):
                unit['掉落'].append({
                    'ID' : CU('4s'),
                    '几率' : CU('i'),
                })
        unit['金钱'] = CU('i')
        unit['位置类型'] = CU('f')
        unit['英雄等级'] = CU('i')

        if obj['subversion'] >= 8:
            unit['力量'] = CU('i')
            unit['敏捷'] = CU('i')
            unit['智力'] = CU('i')

        unit['库存数量'] = CU('i')
        if unit['库存数量'] > 0:
            unit['库存'] = []
            for _ in range(unit['库存数量']):
                unit['库存'].append({
                    '位置' : CU('i'),
                    'ID' : CU('4s'),
                })

        unit['修改技能数量'] = CU('i')
        if unit['修改技能数量'] > 0:
            unit['修改技能'] = []
            for _ in range(unit['修改技能数量']):
                unit['修改技能'].append({
                    'ID' : CU('4s'),
                    '激活' : CU('i'),
                    '等级' : CU('i'),
                })
        
        unit['随机类型'] = CU('i')
        if unit['随机类型'] == 0:
            unit['随机的等级'] = CU('3b')
            unit['随机的物品类型'] = CU('b')
        elif unit['随机类型'] == 1:
            unit['单位组'] = CU('i')
            unit['单位组编号'] = CU('i')
            pass
        elif unit['随机类型'] == 2:
            unit['随机数量'] = CU('i')
            if unit['随机数量'] > 0:
                unit['随机单位'] = []
                for _ in range(unit['随机数量']):
                    unit['随机单位'].append({
                        'ID' : CU('4s'),
                        '概率'  : CU('i'),
                    })
        
        unit['自定义颜色'] = CU('i')
        unit['编号'] = CU('i')
        unit['创建编号'] = CU('i')
        obj['units'].append(unit)
    return obj

def UnpackW3e(Byte):
    CU = UnpackData(Byte)
    obj = {
        'magic_number' : CU('4s'),
        'format_version' : CU('i'),
        '主地面纹理类型' : CU('c'),
        '使用自定义地面纹理组合' : CU('i'),
    }

    obj['使用地面纹理数量'] = CU('i')
    obj['使用地面纹理'] = []
    for _ in range(obj['使用地面纹理数量']):
        obj['使用地面纹理'].append(CU('4s'))

    obj['使用悬崖纹理数量'] = CU('i')
    obj['使用悬崖纹理'] = []
    for _ in range(obj['使用悬崖纹理数量']):
        obj['使用悬崖纹理'].append(CU('4s'))

    obj['地图宽度'] = CU('i')
    obj['地图高度'] = CU('i')
    
    # 地图相对偏移量(原始坐标是地图的左下角0.0,0.0)
    obj['地图水平偏移'] = CU('f')
    obj['地图垂直偏移'] = CU('f')

    obj['tiles'] = []
    for _ in range(int((obj['地图宽度']) * (obj['地图高度']))):
        r_dict = {
            '地面高度' : CU('h'),
            '水面高度 + 边界标志' : CU('h'),
        }
        r_bit = byte2bit(CU(1))
        r_dict['地面纹理'] = int("0b"+ r_bit[4:8],2)
        r_dict['标记值'] = int("0b"+ r_bit[0:4],2)
        r_bit = byte2bit(CU(1))
        r_dict['纹理变化'] = int("0b"+ r_bit[0:5],2)
        r_dict['悬崖变化'] = int("0b"+ r_bit[5:8],2)
        r_bit = byte2bit(CU(1))
        r_dict['悬崖纹理'] = int("0b"+ r_bit[0:4],2)
        r_dict['层高'] = int("0b"+ r_bit[4:8],2)
        obj['tiles'].append(r_dict)
    return obj

def UnpackW3r(Byte):
    CU = UnpackData(Byte)
    obj = {
        'version' : CU('i'),
        'regions_coust' : CU('i'),
    }
    if obj['regions_coust'] > 0:
        obj['regions'] = []
        for _ in range(obj['regions_coust']):
            region = {
                '左锚点' : CU('f'),
                '下锚点' : CU('f'),
                '右锚点' : CU('f'),
                '上锚点' : CU('f'),
            }
            region['名称'] = ""
            while True:
                r_str = CU('s')
                if r_str == '\x00':
                    break
                region['名称'] += r_str
            region['创建编号'] = CU('i')
            region['天气效果'] = CU('4s')
            
            region['环境声音'] = ""
            while True:
                r_str = CU('s')
                if r_str == '\x00':
                    break
                region['环境声音'] += r_str

            CU('x') #未知值
            region['区域颜色'] = {
                'R' : CU('B'),
                'G' : CU('B'),
                'B' : CU('B'),
            }
            obj['regions'].append(region)
    return obj

# 输入结构:
# [
#     {
#      ※ 'ID'
#         '样式'
#      ※ '位置 X'
#      ※ '位置 Y'
#         '位置 Z'
#         '面向角度'
#         '缩放 X'
#         '缩放 Y'
#         '缩放 Z'
#         '可见'
#         '生命'
#     },
# ]
def packDoodad_doo(data):
    Bytes = pack('4s2i',"W3do".encode("utf-8"),8,11)
    Bytes += pack('i',len(data)) # Doodad count
    
    def CreatDoodad():
        DoodadNumber = 0
        def CreatDoodad0(data):
            nonlocal DoodadNumber
            Bytes = pack('4s',data['ID'].encode("utf-8"))
            Bytes += pack('i',data.get('样式',0))
            Bytes += pack('f',data['位置 X'])
            Bytes += pack('f',data['位置 Y'])
            Bytes += pack('f',data.get('位置 Z',0.0))
            Bytes += pack('f',data.get('面向角度',4.71238898038469))
            Bytes += pack('f',data.get('缩放 X',1.0))
            Bytes += pack('f',data.get('缩放 Y',1.0))
            Bytes += pack('f',data.get('缩放 Z',1.0))
            Bytes += pack('b',data.get('可见',2))
            Bytes += pack('b',data.get('生命',100))
            Bytes += pack('i',data.get('掉落表类别',-1))
            Bytes += pack('i',len(data.setdefault('自定义掉落表',[]))) #itemset count
            if len(data['自定义掉落表']) > 0:
                for itemset_count in range(len(data['自定义掉落表'])):
                    Bytes += pack('i',len(data['自定义掉落表'][itemset_count].setdefault('物品',[]))) #item count
                    if len(data['自定义掉落表'][itemset_count]['物品']) > 0 :
                        for item_count in range(len(data['自定义掉落表'][itemset_count]['物品'])):
                            Bytes += pack('4s',data['自定义掉落表'][itemset_count]['物品'][item_count]['ID'])
                            Bytes += pack('i',data['自定义掉落表'][itemset_count]['物品'][item_count]['几率'])
            Bytes += pack('i',DoodadNumber)
            DoodadNumber += 1
            return Bytes
        return CreatDoodad0
    
    CU = CreatDoodad()
    for i in range(len(data)):
        Bytes += CU(data[i])
    Bytes += pack('2i',0,0)
    return Bytes

def UnpackW3xfile(Byte):
    CU = UnpackData(Byte)
    data = {
        'fmt_version' : CU('i'),
        'data' : [],
    }
    for _ in range(2):
        r_data = {
            'object_count' : CU('i'),
            'obj' : [],
        }
        for _ in range(r_data['object_count']):
            each_obj = {
                'original_id' : CU('4s'),
                'modified_id' : CU('4s'),
                'modification' : [],
                'modifications_count' : CU('i'),
            }
            if each_obj['modified_id'] == '\x00\x00\x00\x00' :
                each_obj['modified_id'] = each_obj['original_id']
            for _ in range(each_obj['modifications_count']):
                each_modification = {
                    'modification_id' : CU('4s'),
                    'variable_type' : CU('i'),
                }
                value_type = each_modification['variable_type']
                if value_type == 0 :
                    each_modification['value'] = CU('i')
                elif value_type == 1 or value_type == 2 :
                    each_modification['value'] = CU('f')
                elif value_type == 3 :
                    each_modification['value'] = CU('z')
                
                CU('4x')
                each_obj['modification'].append(each_modification)

            r_data['obj'].append(each_obj)
        data['data'].append(r_data)
    return data

def UnpackW3xfile_has_level(Byte):
    CU = UnpackData(Byte)
    data = {
        'fmt_version' : CU('i'),
        'data' : [],
    }
    for _ in range(2):
        r_data = {
            'object_count' : CU('i'),
            'obj' : [],
        }
        for _ in range(r_data['object_count']):
            each_obj = {
                '模板ID' : CU('4s'),
                '技能ID' : CU('4s'),
                '标签' : [],
                '标签数量' : CU('i'),
            }
            if each_obj['技能ID'] == '\x00\x00\x00\x00' :
                each_obj['技能ID'] = each_obj['模板ID']
            for _ in range(each_obj['标签数量']):
                each_modification = {
                    '标签名' : CU('4s'),
                    '数据类型' : CU('i'),
                }
                each_modification['等级'] = CU('i')
                data_pointer = CU('i')
                if data_pointer > 0:
                    each_modification['data'] = "Data%s%s" % (['A','B','C','D','F','G','H',][data_pointer-1],each_modification['等级'])
                value_type = each_modification['数据类型']

                if value_type == 0 :
                    each_modification['value'] = CU('i')
                elif value_type == 1 or value_type == 2 :
                    each_modification['value'] = CU('f')
                elif value_type == 3 :
                    each_modification['value'] = CU('z')
                
                CU('4x')
                each_obj['标签'].append(each_modification)

            r_data['obj'].append(each_obj)
        data['data'].append(r_data)
    return data

def UnpackW3i(Byte):
    CU = UnpackData(Byte)
    obj = {}
    编码版本 = CU('i')
    地图版本 = CU('i')
    编辑器版本 = CU('i')
    obj['编码版本'] = 编码版本
    obj['地图版本'] = 地图版本
    obj['编辑器版本'] = 编辑器版本
    if 编码版本 >= 28 :
        obj['game_version_major'] = CU('i')
        obj['game_version_minor'] = CU('i')
        obj['game_version_patch'] = CU('i')
        obj['game_version_build'] = CU('i')
    obj['地图名称'] = CU('z')
    obj['作者名字'] = CU('z')
    obj['地图描述'] = CU('z')
    obj['推荐玩家'] = CU('z')

    #镜头
    obj['镜头锚点_左下'] = [CU('f'),CU('f')]
    obj['镜头锚点_右上'] = [CU('f'),CU('f')]
    obj['镜头锚点_左上'] = [CU('f'),CU('f')]
    obj['镜头锚点_右下'] = [CU('f'),CU('f')]
    obj['镜头范围扩充'] = [CU('i'),CU('i'),CU('i'),CU('i')]

    #地形
    obj['地图宽度'] = CU('i')
    obj['地图长度'] = CU('i')

    #选项
    #按位运算
    obj['flags'] = CU('i')

    obj['地形类型'] = CU('s')
    obj['载入图序号'] = CU('i')
    if 编码版本 >= 25 :
        obj['载入图路径'] = CU('z')
    obj['载入图文本'] = CU('z')
    obj['载入图标题'] = CU('z')
    obj['载入图子标题'] = CU('z')

    if 编码版本 >= 25 :
        obj['game_data_set'] = CU('i')

    if 编码版本 == 18 :
        obj['map_loading_screen_number'] = CU('i')

    if 编码版本 >= 25 :
        obj['战役路径'] = CU('z')
    
    obj['战役文本'] = CU('z')
    obj['战役标题'] = CU('z')
    obj['战役子标题'] = CU('z')

    if 编码版本 >= 25 :
        obj['迷雾类型'] = CU('i')
        obj['迷雾开始高度'] = CU('f')
        obj['迷雾结束高度'] = CU('f')
        obj['迷雾密度'] = CU('f')
        obj['迷雾颜色(RBG)'] = {
            "R" : CU('b'),
            "G" : CU('b'),
            "B" : CU('b'),
            "Alpha" : CU('b'),
        }
        
        obj['天气ID'] = CU('i')
        obj['音效'] = CU('z')
        obj['光照'] = CU('b')
        obj['水面颜色(RBG)'] = {
            "R" : CU('b'),
            "G" : CU('b'),
            "B" : CU('b'),
            "Alpha" : CU('b'),
        }

        if 编码版本 >= 28 :
            obj['脚本类型(jass or lua)'] = CU('i')

            if 编码版本 >= 31 :
                obj['supported_modes'] = CU('i')
                obj['game_data_version'] = CU('i')

    players_count = CU('i')
    obj['players'] = []
    for _ in range(players_count):
        player = {
            '玩家编号' : CU('i'),
            '类型' : CU('i'),
            '种族' : CU('i'),
            '修正起始点' : CU('i'),
            '名字' : CU('z'),
            '起始点' : {
                "x" : CU('f'),
                "y" : CU('f'),
            },
            #这里也要按位取值
            'ally_low_priorities_flag' : CU('i'),
            'ally_high_priorities_flag' : CU('i'),
        }
        if 编码版本 >= 31 :
            #这里也要按位取值
            player['enemy_low_priorities_flags'] = CU('i')
            player['enemy_high_priorities_flags'] = CU('i')
        obj['players'].append(player)
    
    forces_count = CU('i')
    obj['forces'] = []
    for _ in range(forces_count):
        force = {
            #这里也要按位取值*2
            "focus_flags" : CU('i'),
            "player_masks" : CU('i'),
            "名字" : CU('z'),
        }
        obj['forces'].append(force)
    
    available_upgrades_count = CU('i')
    obj['upgrades'] = []
    for _ in range(available_upgrades_count):
        upgrade = {
            "player_flags" : CU('i'),
            "id" : CU('4s'),
            "level" : CU('i'),
            "availability" : CU('i'),
        }
        obj['upgrades'].append(upgrade)

    available_tech_count = CU('i')
    obj['techs'] = []
    for _ in range(available_tech_count):
        tech = {
            "player_flags" : CU('i'),
            "id" : CU('4s'),
        }
        obj['techs'].append(tech)

    random_units_table_count = CU('i')
    obj['unit_tables'] = []
    for _ in range(random_units_table_count):
        unit_table = {
            "table_number" : CU('i'),
            "table_name" : CU('z'),
        }
        positions_count = CU('i')
        unit_table['positions'] = []
        for _ in range(positions_count):
            unit_table['positions'].append(CU('i'))

        lines_count = CU('i')
        unit_table['lines'] = []
        for _ in range(lines_count):
            line = {
                "chance" : CU('i')
            }
            for _ in range(positions_count):
                line['id'] = CU('4s')
            unit_table['lines'].append(line)

        obj['unit_tables'].append(unit_table)

    if 编码版本 >= 25 :
        random_items_tables_count = CU('i')
        obj['item_tables'] = []
        for _ in range(random_items_tables_count) :
            item_table = {
                "table_number" : CU('i'),
                "table_name" : CU('z'),
            }
            set_count = CU('i')
            item_table['sets'] = []
            for _ in range(set_count):
                set = []
                item_count = CU('i')
                for _ in range(item_count):
                    item = {
                        "chance" : CU('i'),
                        "id" : CU('z'),
                    }
                    set.append(item)
                item_table['sets'].append(set)
            obj['item_tables'].append(item_table)

    return obj

# “war3map.shd”The Shadow Map File
# 这个文件没有header,，全部都是原始数据。
# 文件大小 = 16*地图宽度*地图高度
# 1byte 可以有两个值:
# 00h =无阴影
# FFh =有阴影
# Each byte set the shadow status of 1/16 of a tileset.
# 也就是说每个tileset 被分为16 (4*4)个部分。def UnpackShd(Byte):
def packShd(Byte):
    _list = []
    for b in Byte:
        #00 FF
        if b != 0:
            _list.append(False)
        else:
            _list.append(True)
            pass
    pass

def UnpackWtg(Byte):
    global r_number
    r_number = 0
    CU = UnpackData(Byte)
    if CU('4s') == 'WTG!':
        obj = {
            'format_version' : CU('i'),
            '触发器类别数量' : CU('i'),
            '触发器类别' : [],
            '变量' : [],
            '触发器' : [],
        }
        for _ in range(obj['触发器类别数量']):
            obj['触发器类别'].append({
                'id' : CU('i'),
                'name' : CU('z'),
                'is_comment' : CU('i'),
            })
        obj['unknown'] = CU('i')
        obj['变量数量'] = CU('i')
        for _ in range(obj['变量数量']):
            obj['变量'].append({
                'name' : CU('z'),
                'type' : CU('z'),
                'unknown' : CU('i'),
                'is_array' : CU('i'),
                'array_size' : CU('i'),
                'is_initialized' : CU('i'),
                'initial_value' : CU('z'),
            })
        obj['触发器数量'] = CU('i')
        for _ in range(obj['触发器数量']):
            trigger = {
                'name' : CU('z'),
                'description' : CU('z'),
                'is_comment' : CU('i'),
                'is_enabled' : CU('i'),
                'is_custom' : CU('i'),
                'is_initially_off' : CU('i'),
                'run_on_initialization' : CU('i'),
                'trigger_category_id' : CU('i'),
                '条目数量' : CU('i'),
                '条目' : [],
            }
            for _ in range(trigger['条目数量']):
                eca = {}
                eca['type'] = CU('i')
                # eca['Group_for_if-then-else'] = CU('i')
                eca['name'] = CU('z')
                eca['is_enabled'] = CU('i')
                eca['parameter'] = []
                for _ in range(5):
                    parameter = {
                        'type' : CU('i'),
                        'value' : CU('z'),
                        '有子参数' : CU('i'),
                        '子参数' : [],
                    }
                    if parameter['有子参数'] == 1:
                        subparameter = {
                            'type' :  CU('i'),
                            'name' : CU('z'),
                            'begin_parameters' : CU('i'),
                        }
                        parameter['子参数'].append(subparameter)
                    # parameter['unknown'] = CU('i'),
                    parameter['is_array'] = CU('i'),
                eca['parameter'].append(parameter)
            obj['触发器'].append(trigger)
        return obj
    else:
        return None

if __name__ == "__main__" :
    with open(r'E:\VScode_project\python_work\预设单位转触发\预设转触发\war3mapUnits.doo','rb') as fi :
        U = UnpackUnit_doo(fi.read())
    with open(r'E:\VScode_project\python_work\预设单位转触发\预设转触发\1.wtg','rb') as fi :
        WTG = UnpackWtg(fi.read())
        _trg = pack('4s','WTG!'.encode("utf-8"))
        _trg += pack('i',7)#format_version
        _trg += pack('i',1)#触发器类别数量
        _trg += pack('i',0)#id
        _trg += pack('9s',"NewUnits".encode("utf-8"))#name
        _trg += pack('i',0) #is_comment
        _trg += pack('i',2)#unknown
        _trg += pack('i',0) #变量数量
        _trg += pack('i',16) #触发器数量
        for i in range(16):
            _trg += pack('8s',('Units%02d' % (i+1)).encode("utf-8")) #name
            _trg += pack('s',''.encode("utf-8")) #description
            _trg += pack('i',0) #is_comment
            _trg += pack('i',1) #is_enabled
            _trg += pack('i',0) #is_custom
            _trg += pack('i',0) #is_initially_off
            _trg += pack('i',0) #run_on_initialization
            _trg += pack('i',0) #trigger_category_id
            eca_count = 0
            # eca
            eca = b''
            for u in U['units']:
                if 'sloc' != u['ID'] and u['玩家'] == i :
                    eca += pack('i',2)
                    eca += pack('12s',' CreateUnit'.encode("utf-8"))
                    eca += pack('i',1) #is_enabled
                    # player
                    eca += pack('i',0)
                    if u['玩家'] >= 12:
                        eca += pack('9s',('Player%s' % (['NA','NV','NE','NP'][u['玩家']-12])).encode("utf-8"))
                    else:
                        eca += pack('9s',('Player%02d' % (u['玩家'])).encode("utf-8"))
                    eca += pack('i',0)
                    eca += pack('i',0)
                    # unitid
                    eca += pack('i',3)
                    eca += pack('5s',u['ID'].encode("utf-8"))
                    eca += pack('i',0)
                    eca += pack('i',0)
                    # X
                    eca += pack('i',3)
                    _str = '%.2f' % (u['位置 X'])
                    eca += pack('%ds' % (len(_str)+1),_str.encode("utf-8"))
                    eca += pack('i',0)
                    eca += pack('i',0)
                    # Y
                    eca += pack('i',3)
                    _str = '%.2f' % (u['位置 Y'])
                    eca += pack('%ds' % (len(_str)+1),_str.encode("utf-8"))
                    eca += pack('i',0)
                    eca += pack('i',0)
                    # R
                    eca += pack('i',3)
                    _str = '%.2f' % (math.degrees(u['面向角度']))
                    eca += pack('%ds' % (len(_str)+1),_str.encode("utf-8"))
                    eca += pack('i',0)
                    eca += pack('i',0)

                    eca += pack('i',0)
                    eca_count += 1
            _trg += pack('i',eca_count)
            _trg += eca
        with open(r'E:\VScode_project\python_work\预设单位转触发\预设转触发\2.wtg','wb') as fi :
            fi.write(_trg)
